package com.example.shop.fgo.ui.fragment;


import android.support.v4.app.Fragment;
import android.widget.ListView;
import android.widget.Toast;

import com.example.shop.fgo.Constant;
import com.example.shop.fgo.R;
import com.example.shop.fgo.base.YBaseFragment;
import com.example.shop.fgo.entity.FileListEntity;
import com.example.shop.fgo.entity.PhotoItem;
import com.example.shop.fgo.net.FileListServe;
import com.example.shop.fgo.ui.adapter.ItemPhotoAdapter;
import com.example.shop.fgo.utils.CookieUtil;
import com.example.shop.fgo.utils.TimeUtil;
import com.example.shop.library.net.NetJsonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class XCFragment extends YBaseFragment implements Callback<FileListEntity> {
    private Map<String, Object> mMapRefreshParams = new HashMap<>();
    private List<FileListEntity.DataEntity> mDataEntities;
    private List<PhotoItem> mPhotoItems;
    private ListView mLvPhoto;
    private List<PhotoItem> mData;
    private ItemPhotoAdapter mItemPhotoAdapter;
    public XCFragment() {
        // Required empty public constructor
    }


    @Override
    public int setRootView() {
        return R.layout.fragment_xc;
    }

    @Override
    public void initViews() {
        mLvPhoto = (ListView) findViewById(R.id.lv_photo);
    }

    @Override
    public void initDatas() {

    }
    private void initRefreshParams() {
        mMapRefreshParams.put("ct", "list");
        mMapRefreshParams.put("aid", "1");
        mMapRefreshParams.put("o", Constant.SORT_USER_PTIME);
        mMapRefreshParams.put("asc", "0");
        mMapRefreshParams.put("offset", "0");
        mMapRefreshParams.put("limit", "120");
        mMapRefreshParams.put("cid", "0");
        mMapRefreshParams.put("type", "2");
    }

    private void doNetRefresh() {
        FileListServe fileListServe = NetJsonUtil.getRetrofit(Constant.BASE_URL, FileListServe.class);
        Call<FileListEntity> call = fileListServe.requestFiles(mMapRefreshParams, CookieUtil.getCookie());
        call.enqueue(this);
    }

    @Override
    public void onResponse(Call<FileListEntity> call, Response<FileListEntity> response) {
        FileListEntity fileListEntity = response.body();
        mDataEntities = fileListEntity.getData();
        mData = getDatas();
        mItemPhotoAdapter=new ItemPhotoAdapter(mActivitySelf,mData);
        mLvPhoto.setAdapter(mItemPhotoAdapter);
        System.out.println(mPhotoItems);
    }

    @Override
    public void onFailure(Call<FileListEntity> call, Throwable t) {
        Toast.makeText(mActivitySelf, "大兄弟，失败了", Toast.LENGTH_SHORT).show();
    }

    public List<PhotoItem> getDatas() {
        if (mPhotoItems==null) {
            mPhotoItems  =new ArrayList<>();
        }


        String time上一个dataEntity="";
        PhotoItem photoItem=null;
        for (FileListEntity.DataEntity dataEntity : mDataEntities) {

            String time这一个dataEntity=TimeUtil.formatTimeNYR(dataEntity.getT());

            if (time上一个dataEntity.equals(time这一个dataEntity)) {
                if (photoItem.isFull()) {
                    photoItem=new PhotoItem();
                    photoItem.addDataEntity(dataEntity);
                    mPhotoItems.add(photoItem);
                }else {
                    photoItem.addDataEntity(dataEntity);
                }

            }else {
                photoItem=new PhotoItem();
                photoItem.setTime(TimeUtil.formatTimeNYR(dataEntity.getT()));
                mPhotoItems.add(photoItem);


                photoItem=new PhotoItem();
                photoItem.addDataEntity(dataEntity);
                mPhotoItems.add(photoItem);
            }
            time上一个dataEntity=time这一个dataEntity;

        }

        return  mPhotoItems;
    }

    @Override
    protected void firstInit() {
        initRefreshParams();
        doNetRefresh();
    }
}
